#include<iostream>
#include<string>
using namespace std;


int strMatching(string str,string substr){
    int a=str.length(),b=substr.length();
    if(a<b)
        return -1;


    int i=0,j=0;
    string astr,bstr;

    while(i<a){
        astr[i]=str[a-i-1];
        i++;
    }
    while(j<b){
        bstr[j]=substr[b-j-1];
        j++;
    }


    i=j=0;
    while(i<a&&j<b){
        if(astr[i]==bstr[j]){
            i++;
            j++;
        }
        else{
            i=i-j+1;
            j=0;
        }
    }

    /*if(j>=b) */return ((a-(i-b+1))-b+2);
//    else return -1;

}

int main()
{
    int pos;
    string substr,str;
    while(1)
    {
        cout<<endl;
        cout<<"input string str: ";
        cin>>str;
        cout<<"input string substr: ";
        cin>>substr;

        pos=strMatching(str,substr);
        if(pos==-1)
                cout<<"cannot find substr in str!"<<endl;
       else
//            cout<<"the first time substr appear in str is at "<<pos<<endl;
            cout<<"the last time substr apppear in str is at: "<<pos<<endl;
    }
}
